package com.example.wificovv2;

import java.util.List;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.example.database.myDBHelper;
import com.example.wificov.SamplingPowPic;
import com.example.wificov.Samplingbandpic;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class SamplingPowMap extends Activity {
	myDBHelper db;
	WifiManager wifi;
	BroadcastReceiver receiver;
	String wifiname ; //= "villa cafe 1";
	GoogleMap googleMap;
    int mapType = GoogleMap.MAP_TYPE_SATELLITE;
    int APID ;
		
	
	// My array
	private TextView ssid;
	private TextView dbindex;
	private EditText typeobs;
	private RadioButton radinside, radoutside;
	private TextView level;
	private Button UpdateWifi, saveUpdateWifi, moveband, backscan;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sampling_pow_map);
		FragmentManager fragmentManager = getFragmentManager();
		final MapFragment mapFragment = (MapFragment) fragmentManager.findFragmentById(R.id.mapfrag);		
		GoogleMap googleMap = mapFragment.getMap();
		
		ssid = (TextView) findViewById(R.id.ssidmap);
		dbindex = (TextView) findViewById(R.id.dbindexmap);
		typeobs = (EditText) findViewById(R.id.typeobsmap);
		radinside = (RadioButton) findViewById(R.id.radinsidemap);
		radoutside = (RadioButton) findViewById(R.id.radoutsidemap);
		UpdateWifi = (Button) findViewById(R.id.UpdateWifi);
		saveUpdateWifi = (Button) findViewById(R.id.saveUpdateWifimap);
		moveband = (Button) findViewById(R.id.movebandmap);
		backscan = (Button) findViewById(R.id.backscanwifi);
		
		String[] apId = {SamplingPowMap.this.getIntent().getStringExtra("arr")};
		String apid = apId[1];
		APID = Integer.parseInt(apid.toString());
		String strName = apId[1];		
		ssid.setText(strName);
		
		wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (wifi.isWifiEnabled() == false)
        {
            wifi.setWifiEnabled(true);
        }  
		
        registerReceiver(new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context c, Intent intent) 
            {
            	List<ScanResult> results = wifi.getScanResults();  
            	wifiname = ssid.getText().toString();
        	    for (ScanResult result : results) {
        	    	if( wifiname.equals(result.SSID)) {
        	    		int lv = result.level;
        	    		dbindex.setText("" + lv);
        	    	}

        		    	
        		}
            }
        }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 
        
				
		saveUpdateWifi.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				

			}
		});
		
		moveband.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// Giữ trạng thái của activity đang đo
				
				// Chuyển sang màn hình lấy bandwidth
				Intent intent = new Intent(SamplingPowMap.this,Samplingbandpic.class);
				startActivity(intent);	

			}
		});
		
		backscan.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// Trước khi quay lại giữ nguyên trạng thái đang đo đạc 
				
				// Chuyển về màn hình lấy vị trí 
				Intent intent = new Intent(SamplingPowMap.this,SeekAp.class);
				startActivity(intent);	

			}
		});
		

	}
	

	/* Request updates at startup */
	@Override
	protected void onResume() {
		super.onResume();
		// locationManager.requestLocationUpdates(provider, 400, 1, this);
	}

	/* Remove the locationlistener updates when Activity is paused */
	@Override
	protected void onPause() {
		super.onPause();
		// locationManager.removeUpdates(this);
	}

//	@Override
//	public void onStop() {
//
//		wifi.End();
//	}

	public void UpdateWifiInfo(List<ScanResult> results) {
		 
//		wifi.StartScan();
		      
		    String message1 = "";
		    String message2 = "";
		    String ssidname = ssid.getText().toString();
		   
		    for (ScanResult result : results) {
		    	message1 += "\n\n" + String.format( "%s ", result.SSID);
		    	if (ssidname ==(message1) ){
			    	message2 += "\n\n" + String.format( "%d ", result.level);
			    	dbindex.setText(message2);
			    	}
		    }
		
	}

	public void clickHandlerForUpdateWifiButton(View v) {
		// Handle Button X here
		wifi.startScan();
//		
	}

	

	// stop
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		super.onCreateOptionsMenu(menu);
		menu.add("Go back home screen").setIntent(
				new Intent(this, MainActivity.class));
		menu.add("Help").setIntent(new Intent(this, Guide.class));
		menu.add("Sampling power").setIntent(
				new Intent(this, SamplingPowPic.class));
		menu.add("Sampling pos and bandwidth").setIntent(
				new Intent(this, SeekAp.class));
		menu.add("View pow map").setIntent(new Intent(this, Displaypow.class));
		menu.add("View band map")
				.setIntent(new Intent(this, Displayband.class));
		menu.add("View wlan map").setIntent(new Intent(this, Wlanmap.class));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mainscreen:
			// Xử lý tro ve man hinh chinh
			break;
		case R.id.guide:
			// xử lý xem thông huong dan
			break;
		case R.id.samplingpow:
			break;
		case R.id.samplingband:
			break;
		case R.id.powermap:
			break;
		case R.id.bandwidthmap:
			break;
		case R.id.wlanmap:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
